<script setup>
import { ref } from 'vue'
import { artChannelService, artDeletaService } from '@/api/article.js'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelEdit from '@/components/ChannelEdit.vue'

const loading = ref(false)
const dialog = ref()

const ChannelList = ref([])
const getChannel = async () => {
  loading.value = true
  const { data } = await artChannelService()
  ChannelList.value = data.data
  loading.value = false
}
getChannel()

const onEditChannel = ($index, row) => {
  dialog.value.open(row)
}
const onDeleteChannel = async ($index, row) => {
  await ElMessageBox.confirm('你确认删除此文章吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDeletaService(row.id)
  getChannel()
}
const onDialog = () => {
  dialog.value.open({})
}
const onSuccess = () => {
  getChannel()
}
</script>
<template>
  <pageContainer title="文章分类">
    <template #extra>
      <el-button plain @click="onDialog" type="primary">添加分类</el-button>
    </template>
    <el-table
      v-loading="loading"
      :data="ChannelList"
      height="400"
      style="width: 100%"
    >
      <el-table-column type="index" label="序号" width="100" />
      <el-table-column prop="cate_name" label="分类名称" />
      <el-table-column prop="cate_alias" label="分类别名" />
      <el-table-column label="操作" width="100">
        <template #default="scope">
          <el-button
            type="primary"
            @click="onEditChannel(scope.$index, scope.row)"
            :icon="Edit"
            circle
          />
          <el-button
            type="danger"
            @click="onDeleteChannel(scope.$index, scope.row)"
            :icon="Delete"
            circle
          />
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <ChannelEdit ref="dialog" @success="onSuccess"></ChannelEdit>
  </pageContainer>
</template>
<style scoped></style>
